﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Royaumes_Libres.Database;

namespace Royaumes_Libres
{
    public partial class ucWorlds : System.Web.UI.UserControl
    {
        public cWorld world;
        public bool isNew;
        public bool isGuest;
        string Lang;
        cUsers pUser;

        protected void Page_Load(object sender, EventArgs e)
        {
            Lang = "Fr";
            if (Session[Properties.Settings.Default.ObjUser] != null)
            {
                pUser = (cUsers)Session[Properties.Settings.Default.ObjUser];
                Lang = pUser.Langue;
            }
            FillDiv();
        }

        protected void FillDiv()
        {
            string Desc;
            if (Lang == "Fr")
            {
                Desc = world.DescFr;
            }
            else
            {
                Desc = world.DescEn;
            }

            if (isGuest)
            {
                this.divJoinWorld.InnerHtml = "<div><h3>" + world.Name + "</h3>" + Desc + "</div>";
            }
            else
            {
                if (!isNew)
                {
                    Button btnJoin = new Button();
                    btnJoin.ID = "btnJoin";
                    btnJoin.Text = cText.GetText("Join");
                    btnJoin.Click += new EventHandler(btnJoin_Click);
                    this.divJoinWorld.InnerHtml = "<div><h3>" + world.Name + "</h3>" + Desc + "</div>";
                    this.divJoinWorld.Controls.Add(btnJoin);
                }
                else
                {
                    Button btnReg = new Button();
                    btnReg.ID = "btnRegWorld";
                    btnReg.Text = cText.GetText("CreateKing");
                    btnReg.CssClass = "btnRegKingdom";
                    btnReg.Click += new EventHandler(btnReg_Click);
                    this.divJoinWorld.InnerHtml = "<div><h3>" + world.Name + "</h3>" + Desc + "</div>";
                    this.divJoinWorld.Controls.Add(btnReg);

                    this.divCreateKingdom.Attributes.Add("class", "divCreateKingdom");
                    this.lstRace.Items.Clear();
                    this.lstRace.Items.Add(cText.GetText("Human"));
                    this.lstRace.Items.Add(cText.GetText("Elf"));
                    this.lstRace.Items.Add(cText.GetText("Dwarf"));
                    this.lstRace.Items.Add(cText.GetText("Orc"));
                    this.btnCreateKingdom.Text = cText.GetText("Create");
                }
            }
        }

        void btnReg_Click(object sender, EventArgs e)
        {
            this.divCreateKingdom.Visible = true;
        }

        void btnJoin_Click(object sender, EventArgs e)
        {
            this.Session.Clear();
            pUser.ActiveWorld = world.PkWorld;
            this.Session.Add(Properties.Settings.Default.ObjUser, pUser);
            Response.Redirect("usermap.aspx");
        }

        protected void btnCreateKingdom_Click(object sender, EventArgs e)
        {
            cKingdom.NewKingdom(txtName.Text, pUser.PkUser, world.PkWorld, lstRace.SelectedIndex + 1);
            this.Session.Clear();
            pUser.ActiveWorld = world.PkWorld;
            this.Session.Add(Properties.Settings.Default.ObjUser, pUser);
            Response.Redirect("usermap.aspx");
        }

    }
}